<?php

require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR ."core".DIRECTORY_SEPARATOR."functions.php");
require_once("Database.php");

class Login extends dbConn
{
	private $id;
	private $email,$password;
	private $salt;
	private $accountType;

	public function __construct($email,$password)
	{
		$this->email = core::sanitize($email);
		$password = core::sanitize($password);
		$this->password = core::sanitize($password);//sha1 hash the password and add the salt to the end

		$qry = "SELECT * FROM person WHERE ".P_E."='$this->email'";
		$res = $this->query($qry);
		if($this->num_rows($res) == 1)
		{
			$data = $this->fetch_array($res);	
			$this->salt = $data['salt'];
			$this->accountType = $data['accountType'];
		}
		else
		{
			//Email not found login failed
			$this->salt = 0;
		}
		$this->password = md5($this->password.$this->salt);
	}
	
	public function tryLogin()
	{
		$qry = "SELECT * FROM person WHERE ".P_E."='$this->email' AND ".P_P."='$this->password'";
		$res = $this->query($qry);
		if($this->num_rows($res) == 1)
		{
			$arr = $this->fetch_array($res);
			$this->id = $arr["personId"];
			return LOGIN_SUCCESS;
		}
		else
		{
			return LOGIN_ERROR;
		}
	}

	public function getId()
	{
		return $this->id;
	}

	public function getAccountType()
	{
		return $this->accountType;
	}
	
	public function getSalt()
	{
		return $this->salt;
	}

} 

?>